import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);
import vuexPersist from "vuex-persist";

export default new Vuex.Store({
  state: {
    shopcar: [],
    user: "",
  },
  mutations: {
    add(state, i) {
      var flag = false;
      state.shopcar.forEach((item) => {
        if (item.id == i.id) {
          flag = true;
          item.num++;
        }
      });
      if (flag == false) {
        Vue.set(i, "num", 1);
        state.shopcar.push(i);
      }
    },
    setuser(state, user) {
      state.user = user;
    },
    exit(state) {
      state.user = "";
    },
    change() {},
    removeall(state, result) {
      state.shopcar = state.shopcar.filter((item) => {
        return result.includes(item) == false;
      });
    },
  },
  actions: {},
  modules: {},
  plugins: [
    new vuexPersist({
      storage: window.localStorage,
    }).plugin,
  ],
});
